package com.qianfeng.xinyihezi.xinyihezi.customview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.activity.GoodsScreenActivity;

/**
 * Created by Tcoy on 2016/11/15.
 */

public class DrawableEditText extends EditText implements View.OnFocusChangeListener {
    private String TAG = getClass().getSimpleName();
    private Drawable drawable;
    private boolean hasFoucs;


    public DrawableEditText(Context context) {
        super(context);
        init();
    }

    public DrawableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawableEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        drawable = getCompoundDrawables()[2];
        if (drawable == null) {
            drawable = getResources().getDrawable(R.mipmap.ic_search);
        }
        drawable.setBounds(0, 0, 40, 40);
        setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {

                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));

                if (touchable) {
                    //里面写上自己想做的事情，也就是DrawableRight的触发事件
//                    this.setText("");//点击icon的时候edtextView 文字改变成这个
                    try {
                        GoodsScreenActivity.handler.sendEmptyMessage(0);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }

                }
            }
        }
        return super.onTouchEvent(event);
    }
}
